home *** CD-ROM | disk | FTP | other *** search
- /*
- gfun.c
-
- Functions to support storing data references
- in a HyperCard global. Allows data to be
- kept between XFCN/XCMD calls.
-
- 11-29-90 1.0d3 JRP extract from XcmdFuncHC.c
-
- There are callbacks in this file:
-
- SetGlobal
- GetGlobal
- */
-
- #include <Memory.h>
- #include <HyperXCmd.h>
- #include <String.h>
-
- #include "gfun.pro"
-
- void convertHanToHexStr(hexStr, han)
- /*
- Convert the Handle to 8-char (+ terminator) hex string representation.
- */
- char *hexStr;
- Handle han;
- {
- short digit,
- shiftCnt = 32;
-
- do
- {
- shiftCnt -= 4;
- digit = '0' + (((short) ( (long) han >> shiftCnt)) & 0x000F);
- if(digit > '9')
- digit += 7; /* A-F hex digit */
- *hexStr++ = digit;
- }
- while(shiftCnt>0);
- *hexStr = '\0'; /* add terminator */
- } /* -------------------------------------------- convertHanToHexStr */
-
- void convertHexStrtoHan(han, hexStr)
- /*
- Convert the hex string representation to a Handle.
- The HexStr representation must be a C-string.
- Only the least-significant 8 hex digits are used.
- Assumes that handle is already locked.
-
- Returns nil for handle if there are not 8 hex digits.
- */
- Handle *han;
- char *hexStr;
- {
- unsigned long digit,
- hanVal=0L;
- short shiftCnt=0,
- i;
- Str255 tempPstr;
-
- if(strlen(hexStr)!=8)
- {
- *han = nil;
- return;
- }
- copyCtoPstr(tempPstr, hexStr);
- for(i=tempPstr[0]; i>0 && shiftCnt < 32; i--)
- {
- digit = tempPstr[i] - '0'; /* unsigned, result is positive */
- if(digit>9L)
- digit -= 7L; /* A-F hex digit */
- if(digit>'F')
- {
- *han = nil;
- return;
- }
- digit <<= shiftCnt; /* shift left */
- hanVal = hanVal | digit; /* insert value */
- shiftCnt += 4;
- }
- *han = (Handle) hanVal;
- } /* -------------------------------------------- convertHexStrtoHan */
-
- void copyCtoPstr(Pstr, Cstr)
- /*
- Copy the Cstr to the Pstr.
- Allows us to make a P-string copy without
- doing it in-place.
- */
- Str255 Pstr;
- char *Cstr;
- {
- short i=0;
-
- while(*Cstr!=0)
- Pstr[++i] = *Cstr++;
- Pstr[0] = i;
- } /* -------------------------------------------- copyCtoPstr */
-
- /*
- Allocate heapspace and copy a C string into it.
- */
- Handle copyStrToHand(str)
- char *str;
- {
- Handle newHndl;
-
- newHndl = (Handle) NewHandle((long) strlen(str) + 1);
- HLock(newHndl);
- strcpy((char *)(*newHndl), str);
- HUnlock(newHndl);
- return(newHndl);
- }
-
- short fetchHanFromGlobal(paramPtr, globalNamePstr, han)
- /*
- Fetch the handle stored in HexStr
- in the HC global container globalName.
-
- 11-29-90 1.0d3 JRP change globalName from C-string to P-string
- add test for empty globalName
-
- Returns result code:
- TRUE successful, found handle
- FALSE handle is nil
- */
- XCmdPtr paramPtr; /* HyperCard connection */
- Str31 globalNamePstr; /* name of global */
- Handle *han; /* pointer to handle */
- {
- Handle hGlobalString; /* string stored in global */
-
- if(globalNamePstr[0]==0)
- *han = nil;
- else
- {
- HLock(hGlobalString = GetGlobal(paramPtr, globalNamePstr));
- convertHexStrtoHan(han, *hGlobalString);
- DisposHandle(hGlobalString);
- }
- return (*han!=nil);
- } /* -------------------------------------------- fetchHanFromGlobal */
-
- short storeHanInGlobal(paramPtr, globalNamePstr, han)
- /*
- Convert the handle to HexStr representation and
- store in the HC global container globalName.
- If handle is zero, store empty in the global.
- This lets the HyperCard script test to see if
- memory has been allocated (not empty == allocated).
-
- 11-29-90 1.0d3 JRP change globalName from C-string to P-string
-
- Returns result code.
- */
- XCmdPtr paramPtr;
- Str31 globalNamePstr;
- Handle han;
- {
- Handle tempHan;
- char hanCstr[16];
-
- if(han==nil)
- *hanCstr = '\0';
- else
- convertHanToHexStr(hanCstr, han); /* convert handle to hex strng */
- tempHan = copyStrToHand(hanCstr); /* store string for passing */
- SetGlobal(paramPtr, globalNamePstr, tempHan); /* store contents */
- DisposHandle(tempHan);
- return MemError();
- } /* -------------------------------------------- storeHanInGlobal */
-
-